<?php

/**
 * This is the model class is useable common class.
 *
 *
 *
 */
class Common extends CActiveRecord {

    /**
     * common 
     * @param string $id .
     * @param string $modelName active record model name.
     * @return  active record find by primary key
     */
    public function loadModelByPK($id, $modelName) {
        $model = $modelName::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function buildSlug($phrase) {
        $sign = array(' ');
        $symbol = array('-');
        $change = str_replace($sign, $symbol, $phrase);
        return strtolower($change);
    }

    public function getOptionValue($type, $name = '') {

        $criteria = new CDbCriteria;
        $params[':type'] = $type;
        $condition = 'option_type=:type';

        if ($name) {
            $params[':name'] = $name;
            $condition .= ' AND option_name=:name';
        }

        $criteria->condition = $condition;
        $criteria->params = $params;

        if ($name) {
            $result = OptionModel::model()->find($criteria);
            if (!empty($result)) {
               return $result->option_value;
            }      
        } else {
            $result = OptionModel::model()->findAll($criteria);
             if (!empty($result)) {
            foreach ($result as $val) {
                $value[$val->option_name] = $val->option_value;
            }
            return $value;
             }       
        }     
        if($params = Yii::app()->params[$type][$name])
            return $params;
            
        
        return false;
    }

}
